<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2017/8/2
 * Time: 10:48
 */

namespace Home\Service;


use Common\Common\Service;
use Home\Component\WeixinComponent;
use Home\Exception\HomeException;
use Home\Request\Weixin\GetSubscribeRequest;
use Home\Response\GetSubscribeResponse;

class GetSubscribeService implements Service
{
    public function run(GetSubscribeRequest $request = null)
    {
        if($request->issetOpenId()==false)
            throw new HomeException(10001);

        $openid = $request->getOpenId();
        $component = new  WeixinComponent();
        $response = new GetSubscribeResponse();
        try {
            $result = $component->getSubscibe($openid);
            $subscribe = $result['subscribe'];
            if (is_null($result) == true) {
                $response->setItIsNull();
            } else {
                $response->setSubscribe($subscribe);
            }
        } catch (HomeException $e) {
            throw new HomeException($e->getMessage(), $e->getCode());
        }
        return $response;
    }
}